﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Abp.Domain.Uow;
using System.Data.SqlClient;
using Abp.Runtime.Caching;
using Dapper;
using System.Threading.Tasks;
using FwProject.Comm.DapperPage;
using System.Linq;
using FwProject.PublicService.SqlDbContext;
using Mondol.DapperPoco;

namespace FwProject.PublicService.SqlExtensions
{
    public static class DapperSqlQueryExtensions
    {
        ///// <summary>
        /////同步方法
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="db"></param>
        ///// <param name="Sql"></param>
        ///// <returns></returns>
        [UnitOfWork(isTransactional: false)]
        public static IList<T> DapperDynamicListFromSql<T>(this Microsoft.EntityFrameworkCore.DbContext db, string Sql, object param = null) where T : class, new()
        {
            SqlContextHelp contextHelp = new SqlContextHelp(db.Database.GetDbConnection().ConnectionString);
            return contextHelp.Query<T>(Sql, param).AsList();
            
        }
        [UnitOfWork(isTransactional: false)]
        public static async Task<IEnumerable<T>> DapperAsyncDynamicListFromSql<T>(this Microsoft.EntityFrameworkCore.DbContext db, string Sql, object param = null) where T : class, new()
        {
            SqlContextHelp contextHelp = new SqlContextHelp(db.Database.GetDbConnection().ConnectionString);
            return await contextHelp.FetchAsync<T>(Sql, param);

        }
        [UnitOfWork(isTransactional: false)]
        public static Paged<T> DapperPageDynamicListFromSql<T>(this Microsoft.EntityFrameworkCore.DbContext db, string sql, string where, int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            SqlContextHelp contextHelp = new SqlContextHelp(db.Database.GetDbConnection().ConnectionString);
            Paged<T> paged = contextHelp.Paged<T>(page, pageSzie, sql);
            return paged;

        }

        [UnitOfWork(isTransactional: false)]
        public static async Task<Paged<T>> DapperPageAsyncDynamicListFromSql<T>(this Microsoft.EntityFrameworkCore.DbContext db , string sql, string where, int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            SqlContextHelp contextHelp = new SqlContextHelp(db.Database.GetDbConnection().ConnectionString);
            Paged<T> paged = await contextHelp.PagedAsync<T>(page, pageSzie, sql);
            return paged;

        }


    }
}
